"use strict";

var mixin_page = {
    data: function data() {
        return {
            // 合约锁
            contract_lock: false,
            user: this.$store.state.user,
            web: this.$store.state.web,
            nav: this.$store.state.nav,
            chain: this.$store.state.web.chain,
            pool: this.$store.state.web.pool,
            dividents: this.$store.state.web.dividents,
            list: [],
            obj: [],
            query: {},
            form: {},
            show: false,
            check_model: "",
            server: true,
            title_submit: "",
            open: true,
            method: "get",
            lang_type: localStorage.getItem('lang_type') || "zh",
            navs: [{
                name: "yasha",
                url: "./index.html"
            },
            {
                name: "invitation"
            },
            {
                name: "invitation_ranking"
            },
            {
                name: "rule"
            },
            {
                name: "tsura",
                url: "../tsura/index.html"
            },
            {
                name: "garuda",
                url: "../garuda/index.html"
            }
            ],
            host: "https://snapshot.yasha.network",
            // host: "http://8.210.94.20:36605"
            // host: "http://192.168.0.114:8080"
        };
    },
    methods: {
        active: function active(name) {
            return location.href.indexOf(name.replace('../', '/').replace('./', '/')) !== -1
        },
        /**
         * 设置语言
         * @param {String} lang_type
         */
        set_lang: function set_lang(lang_type) {
            this.lang_type = lang_type;
            localStorage.setItem('lang_type', lang_type);
        },
        unlock: function () {
            // this.$tron.get_accounts();
            console.log(window.tronWeb && window.tronWeb.defaultAddress);
        },
        /**
         * 语言赋
         * @param {String} key 获取语言
         */
        $t: function (key, ...param) {
            var lang = $.lang[this.lang_type];
            var arr = key.split('.');
            var value = lang;
            for (var i = 0; i < arr.length; i++) {
                // console.log(value, arr[i]);
                value = value[arr[i]]
                if (value === undefined) {
                    console.error("language pack error: " + key + " undefined");
                    break;
                } else if (typeof (value) == "string") {
                    break;
                }
            }

            if (typeof (value) == "string") {
                for (var i = 0; i < param.length; i++) {
                    var reg = new RegExp("\\{" + i + "\\}", 'ig');
                    value = value.replace(reg, param[i]);
                }
            }
            return value;
        },
        /**
         * 提交之后
         * @param {Object} res 返回结果
         * @param {String} tip_success 成功提示
         * @param {String} tip_error 失败提示
         */
        submit_after: function submit_after(res, tip_success, tip_error) {
            if (res.code == 200) {
                this.$message({
                    message: res.message,
                    type: "success"
                });
            } else {
                this.$message({
                    message: res.message,
                    type: "error"
                });
            }
        },

        /**
         * 提交表单
         */
        submit: async function submit() {
            var _this = this;

            var pm = Object.assign({}, this.form);
            var ret = this.check(pm);

            if (!ret) {
                return;
            }

            this.$post(this.url_submit, pm, function (res) {
                _this.submit_after(res);
            });
        },

        /**
         * 获取对象之后
         * @param {Object} obj 对象
         * @return {Object}
         */
        get_obj_after: function get_obj_after(obj) {
            this.form = Object.assign({}, this.obj);
            return obj;
        },

        /**
         * 获取对象
         */
        get_obj: async function get_obj() {
            if (this.url_get_obj) {
                var func;

                if (this.method == "get") {
                    func = this.$get;
                } else {
                    func = this.$post;
                }

                var res = await func(this.url_get_obj, this.query);

                if (res) {
                    var obj = this.get_obj_after(res.data || {});

                    if (obj) {
                        this.obj = obj;
                    }
                }
            }
        },

        /**
         * 获取列表之后
         * @param {Array} arr 列表
         * @return {Array}
         */
        get_list_after: function get_list_after(arr) {
            return arr;
        },

        /**
         * 获取列表
         */
        get_list: async function get_list() {
            if (this.url_get_list) {
                var res = await this.$get(this.url_get_list, this.query);

                if (res) {
                    if (Array.isArray(res)) {
                        var arr = this.get_list_after(res);

                        if (arr) {
                            this.list = arr;
                        }
                    } else if (res.data) {
                        if (Array.isArray(res.data)) {
                            var arr = this.get_list_after(res.data || []);

                            if (arr) {
                                this.list = arr;
                            }
                        } else {
                            var arr = this.get_list_after(res.data.content || []);

                            if (arr) {
                                this.list = arr;
                            }
                        }
                    }
                }
            }
        },

        /**
         * 查询
         */
        get: async function get() {
            await this.get_obj();
            await this.get_list();
        },

        /**
         * 重置表单
         */
        reset: function reset() {
            this.form = Object.assign({}, this.obj);
        },

        /**
         * 表单校验
         */
        check: function check() {
            return true;
        },
        get_balance: async function get_balance() {
            var res = await this.$tron.call('balanceOf', 0, this.$tron.address);
            if (res) {
                var balance = res.toNumber() / this.$tron.config.precision;
                this.$store.commit('user_set', {
                    balance: Math.floor(balance * this.$tron.config.precision) / this.$tron.config.precision
                });
            }
        },
        get_address: async function get_address() {
            var address = await this.$tron.get_address();
            this.$store.commit('user_set', {
                address: address
            });
            return address;
        },

        /**
         * 获取用户信息
         */
        get_user_info: async function get_user_info(func) {
            var address = await this.get_address();

            if (address) {
                await this.get_balance();
            }

            if (func) {
                func();
            }
        },

        /**
         * 初始化
         */
        init: async function init() {
            this.$tron.init();
            await this.get_user_info(this.get);
        },

        $float: function (value, len) {
            if (!len) {
                len = 6;
            }
            return (Math.round(value * Math.pow(10, len)) / Math.pow(10, len)).toFixed(len)
        }
    },
    created: function created() {
        var query = location.href.toQuery();
        this.query = Object.assign(this.query, query); // 初始化

        this.init();
    },
    mounted() {
        $("#app").show()
    }
};